import { parseSvg } from "@meta2d/svg";
import type { Pen } from "@meta2d/core"

export interface CustomPen {
    name: string;
    image: string;
    data?: Pen;
    component?: boolean;
    svg?: string;
    crossOrigin?: string;
}

export async function svgToPens(image: string): Promise<CustomPen> {
    let name = getFileName(image);
    let resData = await fetch(image).then(res => res.text());
    let data = resData
    return {
        name,
        image,
        svg: resData,
        data: parseSvg(data),
        component: true
    }
}

export function pngToPens(image: string): CustomPen {
    let name = getFileName(image);
    let ext = image.split('.').pop()
    return {
        name,
        image,
        data: {
            name: ext,
            // text: name,
            x: 100,
            y: 100,
            width: 100,
            height: 100,
            image,
            imageRatio: true,
            crossOrigin: 'undefined'//默认是'anonymous'
        }
    }
}


function getFileName(path: string) {
    let start = path.lastIndexOf("/");
    let end = path.lastIndexOf(".");
    return path.substring(start + 1, end);
}